<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $name
 * @property string $username
 * @property string $password
 * @property string $email
 * @property string $photo
 * @property string $type
 * @property string $level
 * @property integer $acumulated
 * @property integer $victory
 * @property integer $defeat
 * @property string $activkey
 *
 * The followings are the available model relations:
 * @property Challenge[] $challenges
 * @property Challenge[] $challenges1
 * @property ChallengeHasProposal[] $challengeHasProposals
 * @property Proposal[] $proposals
 * @property Tag[] $tags
 */
class User extends CActiveRecord
{
	public $repeat_password;
	
	public static $badges = array (
			'Youngling' => array(
					'Youngling1' => 'Youngling do Conhecimento Nível 1',
					'Youngling2' => 'Youngling do Conhecimento Nível 2',
					'Youngling3' => 'Youngling do Conhecimento Nível 3',
					'Youngling4' => 'Youngling do Conhecimento Nível 4',
					'Youngling5' => 'Youngling do Conhecimento Nível 5',),
			'Padawan' => array(
					'Padawan1' => 'Padawan do Conhecimento Nível 1',
					'Padawan2' => 'Padawan do Conhecimento Nível 2',
					'Padawan3' => 'Padawan do Conhecimento Nível 3',),
			'JediKnight' => array(
					'JediKnight1' => 'Cavaleiro Jedi do Conhecimento Nível 1',
					'JediKnight2' => 'Cavaleiro Jedi do Conhecimento Nível 2',
					'JediKnight3' => 'Cavaleiro Jedi do Conhecimento Nível 3',),
			'JediMaster' => array('JediMaster1' => 'Mestre Jedi do Conhecimento'),
				
				
	);
	
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, username, password, repeat_password, email', 'required'),
			array('acumulated, victory, defeat', 'numerical', 'integerOnly'=>true),
			array (	'password',	'compare','compareAttribute' => 'repeat_password'),
			array('name, email, photo, activkey', 'length', 'max'=>250),
			array('username, password, type, level', 'length', 'max'=>45),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, username, password, email, photo, type, level, acumulated, victory, defeat, activkey', 'safe', 'on'=>'search'),
		);
	}
	
	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave()
	{
		parent::afterSave();
		echo $this->id;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'challenges' => array(self::HAS_MANY, 'Challenge', 'challenger_id'),
			'challenges1' => array(self::HAS_MANY, 'Challenge', 'mediator_id'),
			'challengeHasProposals' => array(self::HAS_MANY, 'ChallengeHasProposal', 'challenged_id'),
			'proposals' => array(self::HAS_MANY, 'Proposal', 'user_id'),
			'tags' => array(self::MANY_MANY, 'Tag', 'ranking_by_tag(user_id, tag_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Nome',
			'username' => 'Usuário',
			'password' => 'Senha',
			'repeat_password' => 'Repetição de Senha',
			'email' => 'Email',
			'photo' => 'Foto',
			'type' => 'Tipo',
			'level' => 'Nível',
			'acumulated' => 'Vitórias Acumuladas',
			'victory' => 'Victoria',
			'defeat' => 'Derrota',
			'activkey' => 'Chave de Ativação',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('photo',$this->photo,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('level',$this->level,true);
		$criteria->compare('acumulated',$this->acumulated);
		$criteria->compare('victory',$this->victory);
		$criteria->compare('defeat',$this->defeat);
		$criteria->compare('activkey',$this->activkey,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
